const SYSTEM_LOGIN_USER_KEY = 'systemLoginUserInfo';
const SYSTEM_INFO_KEY = 'systemInfo';

/**
 * 获取token
 */
export function getSystemUriToken() {
  const localSystemInfo = getLocalStorageByKey(SYSTEM_INFO_KEY);
  const key = getSystemInfoKey();
  return (localSystemInfo && localSystemInfo[key] && localSystemInfo[key].token) || null;
}

export function getSystemInfoKey() {
  const systemUri = getSystemUri();
  const userId = getUrlUserId();
  return window.systemInfoKey || window.top.systemInfoKey || `${systemUri}_${userId}`;
}

export function getSystemUri() {
  console.log(window.top.systemUri, window.systemUri);
  const systemUrl = window.systemUri || window.top.systemUri;
  return systemUrl || (window.location.pathname === '/portal' ? 'PORTAL' : null);
}

export function getLocalStorageByKey(key) {
  if (!key) return;
  const localValue = window.localStorage.getItem(key);
  let value = null;
  try {
    value = JSON.parse(localValue);
  } catch (e) {
    if (e) {
      console.error(e);
    }
  }
  return value;
}

export function getSystemInfoByKey(key) {
  const localSystemInfo = getLocalStorageByKey(SYSTEM_INFO_KEY);
  const locallKey = getSystemInfoKey();
  console.log('localSystemInfo', localSystemInfo, locallKey);
  const systemInfo = localSystemInfo[locallKey];
  return systemInfo[key];
}

export function getUrlUserId() {
  return window.userId || window.top.userId || null;
}
